﻿using Auth0;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WorkBasket.Data.Entities;
using WorkBasket.Data.Repository;
using WorkBasket.Infrastructure;
using WorkBasket.Models;

namespace WorkBasket.Controllers
{
    public class InviteResponseController : Controller
    {
        // GET: Invite
        public ActionResult Index(string InviteID)
        {
            SOC soc = new SOC(new MyContext());

            InviteModel im = new InviteModel();
            
            Invite invite = soc.Invites.Where(c => c.UniqueID.ToString() == InviteID  && c.Responded == null).FirstOrDefault();
            if(invite==null)
                return Redirect("/"); //eventually will need to tell them why it didn't work but for now just send them home


            im.Invite = invite;

            return View(im);
        }

        [HttpPost]
        public ActionResult Accept(InviteModel InviteModel)
        {
            if(ModelState.IsValid){

                UserProfile user = AuthFunctions.CreateUser(InviteModel.Invite.EMail, InviteModel.Password);


                SOC soc = new SOC(new MyContext());
                Invite invite = soc.Invites.GetByID(InviteModel.Invite.InviteID);

                invite.UserID = user.UserId;
                invite.Accepted = true;
                invite.Responded = DateTime.Now;


 
                soc.Invites.Update(invite);
                soc.CommitChanges();





                return Redirect("/dash");
          }else
            {
                return RedirectToAction("Index", InviteModel.Invite.UniqueID);
            }

        }
        [HttpGet]
        [ActionName("Decline")]
        public ActionResult Decline(int InviteID= 0)
        {
            if(InviteID==0)
                return Redirect("/");
            SOC soc = new SOC(new MyContext());
            Invite invite = soc.Invites.GetByID(InviteID);

 
            invite.Accepted = false;
            invite.Responded = DateTime.Now;


           

            soc.Invites.Update(invite);
            soc.CommitChanges();

 

            return Redirect("/");
        }
    }
}